// pages/recruitedit/recruitedit.js
var app = getApp();
const chooseLocation = requirePlugin('chooseLocation');
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		id:'',
		phone:'',
		jobtype:{
			text:'长期或替班都行',
			value:0,
			popup:false
		},
		pay:0,
		isfare:0,
		issafe:0,
		op:{
			text:'正反都可',
			value:0,
			popup:false
		},
		desc:'',
		descsize:{minHeight: 80 },
		InitCity:{
			popup:false,
			component:false,
			province:'',
			city:'',
			lastname:'',
			latitude:0,
			longitude:0,
			isprovince:0,
			iscity:0
		},
		shebei:{
			popup:false,
			param:{
				activeId: 0,
				activeText:'',
			},
			component:false
		},
	},
	ShebeiPopupOpen(){
		this.setData({
			"shebei.popup":true,
			"shebei.component":true
		});
		
	},
	ShebeiChangeVal(val){
		var sid = val.detail.sid;
		var text = val.detail.text;
		this.ShebeiPopupClose();
		if(parseInt(this.data.shebei.param.activeId) != parseInt(sid)){
			this.setData({
				"shebei.param.activeId":sid,
				"shebei.param.activeText":text
			});
		}
	},
	ShebeiPopupClose(){
		this.setData({
			"shebei.popup":false
		});
	},
	ShebeiPopupAfterLeave(){
		this.setData({
			"shebei.component":false
		});
	},
	cityPopupOpen(){
		this.setData({
			"InitCity.popup":true,
			"InitCity.component":true
		});
		var latitude = wx.getStorageSync("latitude");
		var longitude = wx.getStorageSync("longitude");
		if(latitude&&longitude){
			this.OpentLocation(latitude,longitude)
		}else{
			wx.getLocation({
			    type: 'wgs84',
			    success :(res) => {
					this.OpentLocation(res.latitude,res.longitude)
			    },
				fail:(res) =>{
					wx.showModal({
						title: '提示',
						content: '位置请求频繁，请稍后重试！',
						showCancel:false,
						success (res) {
					    if (res.confirm) {
							// console.log('用户点击确定')
					    } else if (res.cancel) {
							// console.log('用户点击取消')
					    }
					  }
					})
				}
			})
		}
	},
	OpentLocation(latitude,longitude){
		const key = 'YBCBZ-WC4CU-P4EVZ-25TQK-UZAKE-XQBKR'; //使用在腾讯位置服务申请的key
		const referer = '75带班'; //调用插件的app的名称
		const location = JSON.stringify({
		  latitude: latitude,
		  longitude: longitude
		});
		
		wx.navigateTo({
		  url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}`
		});
	},
	ChangeCityVal(val){
		this.setData({
			"InitCity.province":val.province,
			"InitCity.city":val.city,
			"InitCity.lastname":val.lastname,
			"InitCity.latitude":val.latitude,
			"InitCity.longitude":val.longitude
		});
	},

	onOpenJobtype(){
		this.setData({
		  'jobtype.popup': true
		});
	},
	onCloseJobtype(){
		this.setData({
		  'jobtype.popup': false
		});
	},
	handleJobtypeClick(event){
		var val = parseInt(event.target.dataset.val);
		var text = ''
		if(val == 2){
			text = '长期';
		}else if(val == 1){
			text = '替班';
		}else{
			text = '长期或替班都行';
		}
		this.setData({
			'jobtype.value': val,
			'jobtype.text': text
		});
	},

	handleisfareChange(val){
		this.setData({
			'isfare': val.detail,
		});
	},
	handleissafeChange(val){
		this.setData({
			'issafe': val.detail,
		});
	},
	onOpenop(){
		this.setData({
		  'op.popup': true
		});
	},	
	onCloseop(){
		this.setData({
		  'op.popup': false
		});
	},	
	handleOpClick(event){
		var val = parseInt(event.target.dataset.val);
		var text = ''
		if(val == 2){
			text = '反手';
		}else if(val == 1){
			text = '正手';
		}else{
			text = '正反都可';
		}
		this.setData({
			'op.value': val,
			'op.text': text
		});
	},
	
	
	onheaderClickLeft(){
		wx.navigateBack({})
	},
	formSubmit(){
		var data = this.data;
		const token = wx.getStorageSync('token');
		if(data.phone){
			var status = app.checkPhone(data.phone);
			if(!status){
				wx.showToast({
					title: '手机号格式错误',
					icon: 'error',
					duration: 2000
				})
				return false;
			}
		}else{
			wx.showToast({
				title: '联系电话不能为空',
				icon: 'error',
				duration: 2000,
			});
			return false;
		}
		if(!data.InitCity.province || !data.InitCity.city){
			wx.showToast({
				title: '请填写工作地点',
				icon: 'error',
				duration: 2000,
			});
			return false;
		}
		if(!data.desc){
			wx.showToast({
				title: '招聘信息不能为空',
				icon: 'error',
				duration: 2000,
			});
			return false;
		}
		
		wx.showLoading({
		  title: '提交中',
		});
	
		var param = {
			token:token,
			phone:data.phone,
			province:data.InitCity.province,
			city:data.InitCity.city,
			jobtype:data.jobtype.value,
			lastname:data.InitCity.lastname,
			latitude:data.InitCity.latitude,
			longitude:data.InitCity.longitude,
			sid:data.shebei.param.activeId || 0,
			pay:data.pay,
			isfare:data.isfare,
			issafe:data.issafe,
			op:data.op.value,
			desc:data.desc
		};
		if(data.id == 'add'){
			wx.request({
				url: 'https://www.grabinter.com/Works/addworks',
				method: 'POST',
				header: {
					"Content-Type": 'application/x-www-form-urlencoded'
				},
				data: param,
				success: json => {
					if(json.data.success){
						wx.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 2000,
						});
						setTimeout(function(){
							wx.navigateBack({})
						},2000);
					}else{
						wx.showToast({
							title: '提交失败',
							icon: 'error',
							duration: 2000,
						});
						
					}
				}
			});
		}else{
			param['id'] = data.id;
			param['edit'] = true;
			wx.request({
				url: 'https://www.grabinter.com/Works/editworks',
				method: 'POST',
				header: {
					"Content-Type": 'application/x-www-form-urlencoded'
				},
				data: param,
				success: json => {
					if(json.data.success){
						wx.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 2000,
						});
						setTimeout(function(){
							wx.navigateBack({})
						},2000);
					}else{
						wx.showToast({
							title: '提交失败',
							icon: 'error',
							duration: 2000,
						});
						
					}
				}
			});
		}
		
	},
	Getdata(){
		var self = this;
		wx.showLoading({
			mask:true,
			title: '加载中',
		});
		wx.request({
			url: 'https://www.grabinter.com/Works/editworks',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: {
				token:wx.getStorageSync('token'),
				id:this.data.id
			},
			success: json => {
				wx.hideLoading()
				if(json.data.data){
					var data = json.data.data;
					var jobtypetext = '';
					if(parseInt(data.jobtype) == 2){
						jobtypetext = '长期';
					}else if(parseInt(data.jobtype) == 1){
						jobtypetext = '替班';
					}else{
						jobtypetext = '长期或替班都行';
					}
					var optext = '';
					if(parseInt(data.op) == 2){
						optext = '反手';
					}else if(parseInt(data.op) == 1){
						optext = '正手';
					}else{
						optext = '正反都可';
					}
					var shebeitext = '';
					if(parseInt(data.sid)>0){
						for(var i in app.globalData.shebei){
							for(var x in app.globalData.shebei[i].children){
								const itgem = app.globalData.shebei[i].children[x];
								if(itgem.sid == data.sid){
									shebeitext = itgem.text
								}
							}
						}
					}
					self.setData({
						phone:data.phone,
						'jobtype.value':parseInt(data.jobtype),
						'jobtype.text':jobtypetext,
						pay:parseInt(data.pay),
						'shebei.param.activeId':parseInt(data.sid),
						'shebei.param.activeText':shebeitext,
						isfare:parseInt(data.isfare),
						issafe:parseInt(data.issafe),
						'op.value':parseInt(data.op),
						'op.text':optext,
						'InitCity.province':data.province,
						'InitCity.city':data.city,
						desc:data.desc,
					});
				}else{
					wx.showToast({
						title: '获取数据失败',
						icon: 'error',
						duration: 2000,
					});
					setTimeout(function(){
						wx.navigateBack({})
					},2000)
				}
			}
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let {id,name} = options;
		this.setData({
			id:id
		});
		if(id == 'add'){
			// var DefaultCity = app.globalData.DefaultCity;
			// if(DefaultCity != 'not'){
			// 	DefaultCity = DefaultCity.split(',');
				
			// 	this.setData({
			// 		"InitCity.province":DefaultCity[0],
			// 		"InitCity.city":DefaultCity[1]
			// 	})
			// }
		}else{
			this.Getdata();
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
		if(location){
			console.log(location)
			var fprovince = '';
			var fcity = '';
			if(location.province.indexOf('市')>-1){
				fprovince = location.province.split("市").join("");
			}
			if(location.province.indexOf('省')>-1){
				fprovince = location.province.split("省").join("");
			}
			if(location.city.indexOf('市')>-1){
				fcity = location.city.split("市").join("");
			}
			this.ChangeCityVal({
				province:fprovince,
				city:fcity,
				lastname:location.name,
				latitude:location.latitude,
				longitude:location.longitude,
			});
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
